﻿@{
    ViewBag.Title = "ShowPage";
    Layout = "~/Views/Shared/_LayoutChild.cshtml";
}
@section head {
    @if (false)
    {<script src="~/Scripts/jquery-1.6.4-vsdoc.js" type="text/javascript"></script>}
    @Html.Partial("_JqGrid")
    <script language="javascript" type="text/javascript">
        var selectId = "";
        var gridId = "#tableId";
        var curSelectNode = null;
        var Userid = "";
        var _rowId = "";
        $(document).ready(function () {
            //将输入的条件自动转为大写
            $("#KeyCode").keyup(function () {//keyup当按键弹起时触发事件
                $(this).css("background-color", "#D6D6FF").val($(this).val().toUpperCase());
            });
            $("#KeyCode").keydown(function (even) {
                $(this).css("background-color", "#FFFFCC");
                var curKey = even.which;
                if (curKey == 13)
                    ExecSql();
            });
        });

        //计算字符串的占用字节数，中文两个字节，英文一个字节
        function getStrActualLen(val) {
            var len = 0;
            for (var i = 0; i < val.length; i++) {
                if (val[i].match(/[^x00-xff]/ig) != null) //全角 
                    len += 2;
                else
                    len += 1;
            }
            if (len < 5) len = 5; //如果内容长度太短，刚置为5
            return len;
        }




        //通过发回来的jqgrid json数据，动态构建jqgrid 的colModel
        function CreatcolModel(d) {
            var colModel = "[";
            $.each(d.rows, function (i, field) {
                if (i < 1) {
                    var count = 0;
                    $.each(field, function (j, d) {

                        if (count <= 0)
                            colModel += "{label: '" + j + "', name: '" + j + "', width:'" + getStrActualLen(d == null ? j : d) * 7 + "px',sortable:true}"; //如果内容不为空，刚根据内容长度计算显示的列宽，否则根据标题长度计算
                        else
                            colModel += ",\n{label: '" + j + "', name: '" + j + "',width:'" + getStrActualLen(d == null ? j : d) * 7 + "px',sortable:false}";
                        count++;

                    });
                }
            });
            colModel += "]";

            return colModel;
        }


        function ExecSql() {
            var KeyCode = $("#KeyCode").val();
            if ($.trim(KeyCode) == "") return;
            $.post("/SystemManage/ExecSql/GetResult", { KeyCode: KeyCode }, function (d) {
                if (d.ErrorMessage == null || d.ErrorMessage == "") {

                    var colModel = CreatcolModel(d); //根据返回数据，动态生成列名
                    funGridInit(colModel);
                    var thegrid = $(gridId)[0];
                    thegrid.addJSONData(d); //将数据填充到grid中
                }
                else {
                    GridUnload();
                    alert("查询失败:" + d.ErrorMessage);
                }
            });
        }



        function funGridInit(colModel) {

            //初始化表格
            var option = {
                height: $(document).height() - 110,
                width: $(document).width() - 3,
                //                url: '/SystemManage/ExecSql/GetResult',
                autowidth: false,
                datatype: "local",
                altRows: true, //隔行变色
                altclass: "jqgrid-row",
//                sortname: "File_ID",
//                sortorder:"desc",
//                sortable: true,
                colModel: eval(colModel),
                beforeRequest: function (d) {

                    //$(gridId).jqGrid('setGridParam', { postData: { KeyCode: $("#KeyCode").val()} });
                    //                     $(gridId).jqGrid('setGridParam',{colModel:colModel}); 
                    //                    $(gridId).jqGrid('hideCol',"Report_ID");

                }
            };
            GridUnload();
            var o = jqGridHelper.getConfig("#pageId", option);
            $(gridId).jqGrid(o);
        }


        function GridDestroy() {
            //从dom上删除此grid
            $(gridId).GridDestroy();
        }

        function GridUnload() {   //跟GridDestroy不同的是table对象跟pager对象并不会被删除，以便下次使用
            $(gridId).GridUnload();
        }

    </script>
}
<div class="ui-layout-north">
    <div id="headertitle">
    </div>
    @*<div class="buttonlist" style="float: right; height: 34px; line-height: 36px; margin-right: 20px;">
        <input type="button" class="button" value="GridDestroy" style="font-size: 12px;"
            onclick="GridDestroy()" />
        <input type="button" class="button" value="GridUnload" style="font-size: 12px;" onclick="GridUnload()" />
    </div>*@
</div>
<!--头部导航,不用时可以去掉-->
<!--主窗体，页面都需要的div-->
<div class="ui-layout-center" style="overflow: hidden;">
    <div>
        @Html.Label("Sql语句:")
        @Html.TextBox("KeyCode", "SELECT * FROM T_REPORT", new { @style = "width:80%" })
        <input type="button" id="btnExec" value="执行" onclick="ExecSql();" />
    </div>
    <div id="divGridContainer" style="overflow: hidden;">
        <table id="tableId">
        </table>
        <div id="pageId">
        </div>
    </div>
</div>
